package org.iubbo.proxy.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.service.GenericService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hundsun.jrescloud.rpc.api.IRpcProxyFactory;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.iubbo.proxy.config.ReferenceFactory;
import org.iubbo.proxy.model.dto.DubboInvokerParamDTO;
import org.iubbo.proxy.service.DubboInvokeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.iubbo.proxy.config.CommonConstants.SAMPLE_REQ;

/**
 * DUBBO的invoke服务封装
 *
 * @author idea
 * @date 2020/1/2
 * @version V1.0
 */
@Service
public class DubboInvokeServiceImpl implements DubboInvokeService {

    @Autowired
    private IRpcProxyFactory iRpcProxyFactory;

    @Override
    public Map<String, String> getAllParamsMap() {
        return null;
    }

    @Override
    public Object doInvoke(DubboInvokerParamDTO param) {
        //String keys = JSONArray.toJSONString(param.getArgKeys());
        //String params = JSONArray.toJSONString(param.getArgObjects());
        Map<String, Object> map = new HashMap<>();
        for (int i = 0;i<param.getArgKeys().length;i++) {
            map.put(param.getArgKeys()[i],param.getArgObjects()[i]);
        }
        String params = JSONObject.toJSONString(map);
        //param.setInterfaceName("com.hundsun.isms6.fundmodu.api.service.FundOpenInfoService");
        //param.setMethodName("com.hundsun.isms6.fundmodu.api.service.FundOpenInfoService.listFundOpenInfoByFundId");
        param.setMethodName(param.getInterfaceName() + "." + param.getMethodName());
        Object result = iRpcProxyFactory.genericInvokeJresT3(param.getInterfaceName(),param.getGroup(),param.getVersion(),param.getMethodName(),params);

        return result;
    }

    @Override
    public String checkFields(DubboInvokerParamDTO param) {
        if (param.getInterfaceName() == null) {
            return "interfaceName is Required, sample request = \n" + SAMPLE_REQ;
        }

        if (param.getMethodName() == null) {
            return "methodName is Required, sample request = \n" + SAMPLE_REQ;
        }

        if (param.getArgTypes() == null) {
            param.setArgTypes(new String[]{});
        }

        if (param.getArgKeys() == null) {
            param.setArgKeys(new String[]{});
        }

        if (param.getArgObjects() == null) {
            param.setArgObjects(new Object[]{});
        }

        if (param.getArgTypes().length != param.getArgObjects().length) {
            return "paramTypes.length is not equal to paramArgs.length, sample request = \n" + SAMPLE_REQ;
        }

        return null;
    }

}
